package com.spzx.channel.service.impl;

import com.alibaba.fastjson.JSON;
import com.spzx.channel.domain.ItemVo;
import com.spzx.channel.service.IItemService;
import com.spzx.common.core.domain.R;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

@Service
public class IItemServiceImpl implements IItemService {
    @Autowired
    RemoteProductService remoteProductService;
    @Override
    public ItemVo item(Long skuId) {
        //查询Product_sku
        ProductSku productSku = remoteProductService.getProductSku(skuId, "inner").getData();
        Long productId = productSku.getProductId();

        //查询Product
        Product product = remoteProductService.getProduct(productId, "inner").getData();
        //查询Product_details
        ProductDetails productDetails = remoteProductService.getProductDetails(productId, "inner").getData();
        //查询Product_spec
        Map<String, Long> specMap = remoteProductService.getSkuSpecValue(productId, "inner").getData();
        //查询Sku_stock
        SkuStockVo skuStockVo = remoteProductService.getSkuStock(skuId, "inner").getData();
        //查询price
        SkuPrice skuPrice = remoteProductService.getSkuPrice(skuId, "inner").getData();

        ItemVo itemVo = new ItemVo();
        itemVo.setProduct(product);
        itemVo.setProductSku(productSku);
        itemVo.setSkuPrice(skuPrice);
        itemVo.setSkuStockVo(skuStockVo);

        String imageUrls = productDetails.getImageUrls();
        itemVo.setDetailsimagesUrlList(Arrays.asList(imageUrls.split(",")));
        String specValue = product.getSpecValue();
        itemVo.setSpecValueList(JSON.parseArray(specValue));

        String sliderUrls = product.getSliderUrls();
        itemVo.setSliderUrlList(Arrays.asList(sliderUrls.split(",")));

        itemVo.setSkuSpecValueMap(specMap);
        return itemVo;
    }
}
